package action;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.Transaction;

import bean.Score;
import bean.ScoreId;
import util.HibernateUtil;

public class Test_CompositeId {

	public static void main(String[] args) {
		// Test_CompositeId.select();

		// Test_CompositeId.save();

		Test_CompositeId.update();
	}

	/**
	 * 查找
	 */
	private static void select() {

		Session session = null;
		Transaction tran = null;

		ScoreId id = new ScoreId();
		id.setUserid(1);
		id.setSubject("语文");

		try {
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			tran = session.beginTransaction();

			Score score = (Score) session.get(Score.class, id);

			System.out.println("成绩=" + score.getScore());

			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}

	}

	private static void save() {
		Session session = null;
		Transaction tran = null;

		ScoreId id = new ScoreId();
		id.setSubject("英语");
		id.setUserid(1);

		Score score = new Score();
		score.setId(id);
		score.setScore(100);

		try {
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			tran = session.beginTransaction();

			Serializable primarykey = session.save(score);
			System.out.println(primarykey);

			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}
	}

	/**
	 * 执行更新操作
	 */
	private static void update() {

		Session session = null;

		Transaction tran = null;

		ScoreId id=new ScoreId();
		id.setUserid(1);
		id.setSubject("英语");
		
		Score score=new Score();
		score.setId(id);
		score.setScore(12);
		
		try {
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			tran = session.beginTransaction();
			
			session.update(score);
			
			
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}

	}

}
